/** HDU 1950 Longest Increasing Sequence */
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 40000 + 7;
int n, p, a[maxn], b[maxn], cnt;
int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  cin>>n;
  while(n--) {
    cnt = 0;
    cin>>p; for(int i=0; i<p; ++i) cin>>a[i];
    b[++cnt] = a[0];
    for(int i=0; i<p; ++i) {
      if(a[i]>b[cnt]) {
        b[++cnt] = a[i];
      }
      else {
        int pos = lower_bound(b+1, b+1+cnt, a[i])-b;
        if(pos<=cnt) {
          b[pos] = a[i];
        }
      }
    }
    for(int i=0; i<=cnt; ++i) cout<<b[i]<<' ';
    cout<<endl;
    cout<<cnt<<'\n';
  }
}

